﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using QuanLyThuoc.BUS;
namespace QuanLyThuoc.PresentionLayer
{
    public partial class frmNhanVien : Form
    {
        public int keyValue = 0;
        int vitri = 0;
        bool plAddMode = false;
        bool plEditMode = false;
        BindingSource dsNV;
        public frmNhanVien()
        {
            InitializeComponent();
        }
        private void ResetControl()
        {
            this.txtDiachi.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtMK.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtSodt.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtIDNV.ReadOnly = !(this.plAddMode || this.plEditMode);
            this.txtTenNV.ReadOnly = !(this.plAddMode || this.plEditMode);

            this.cmdAdd.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.save24 : QuanLyThuoc.Properties.Resources.Add24;
            this.cmdEdit.Image = (this.plAddMode || this.plEditMode) ? QuanLyThuoc.Properties.Resources.undo24 : QuanLyThuoc.Properties.Resources.Edit24;

            this.cmdAdd.Text = (this.plAddMode || this.plEditMode) ? "Lưu [F2]" : "Thêm [F2]";
            this.cmdEdit.Text = (this.plAddMode || this.plEditMode) ? "Hủy [F4]" : "Sửa [F4]";

            this.grdList.Enabled = !this.plAddMode;

        }
        private void cmdAdd_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
                this.Save();
            else
                this.AddBlank();

            this.plAddMode = !this.plAddMode;
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void frmNhanVien_Load(object sender, EventArgs e)
        {
            this.HienThiDanhSach();
            this.ResetControl();
        }
        private void HienThiDanhSach()
        {
            dsNV = new BindingSource();
            dsNV.DataSource = NhanVienBUS.SelectAll_NhanVien();
            grdList.DataSource = dsNV;
            int i;
            for (i = 0; i < grdList.Rows.Count; i++)
            {
                grdList.Rows[i].HeaderCell.Value = (i + 1);
            }
        }
       private void GanDK(DataGridViewRow r)
        {
            try
            {
                if (grdList.Rows.Count != 0)
                {
                    this.txtIDNV.Text = r.Cells[0].Value.ToString();
                    this.txtTenNV.Text = r.Cells[1].Value.ToString();
                    this.txtMK.Text = r.Cells[2].Value.ToString();
                    this.txtDiachi.Text = r.Cells[3].Value.ToString();
                    this.txtSodt.Text = r.Cells[4].Value.ToString();
                }
                else
                {
                    this.txtIDNV.Text = "";
                    this.txtTenNV.Text = "";
                    this.txtMK.Text = "";
                    this.txtDiachi.Text = "";
                    this.txtSodt.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi:" + ex.Message);
            }
        }
        private void AddBlank()
        {

            this.txtIDNV.Enabled = false;
            this.txtTenNV.Text = "";
            this.txtMK.Text = "";
            this.txtDiachi.Text = "";
            this.txtSodt.Text = "";
            this.txtTenNV.Focus();
            // HienThiDanhSachNhaCungCap();
        }
        private void Save()
        {
            //int MaNhaCC = Convert.ToInt32(txtIDNCC.Text.ToString());
            string TenNV = txtTenNV.Text;
            string DiaChi = txtDiachi.Text;
            string SoDT = txtSodt.Text;
            string Matkhau = txtMK.Text;


            if (string.IsNullOrEmpty(this.txtIDNV.Text.Trim()))
            {
                NhanVienBUS.InsertNhanVien(TenNV, DiaChi, Matkhau, SoDT);
                if (grdList.RowCount > 0)
                {
                    DataGridViewRow r = this.grdList.SelectedRows[0];
                    this.AddBlank();

                    Gan_Click();
                }
            }
            else
            {
                int MaNV = Convert.ToInt32(txtIDNV.Text.ToString());

                NhanVienBUS.UpdateNhanVien(MaNV, TenNV, DiaChi, Matkhau, SoDT);
            }
           
        }

        private void AddNew()
        {
            this.plAddMode = true;
            this.plEditMode = false;
           

        }
        private void Edit()
        {
            this.plEditMode = true;
            this.plAddMode = false;
        }
        private void Undo()
        {
            //this.bmbList.CancelCurrentEdit();
            vitri = grdList.CurrentCell.RowIndex;
            this.plAddMode = false;
            this.plEditMode = false;
        }
        private void Delete()
        {
            try
            {
                int maNV = int.Parse(txtIDNV.Text.ToString());
               
                    if (MessageBox.Show("Bạn có muốn xóa nhà cung cấp '" + txtTenNV.Text + "'không ??", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        if (vitri == grdList.RowCount)
                        {
                            NhanVienBUS.DeleteNhanVien(maNV);
                            this.AddBlank();
                            grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                            grdList.Rows[vitri - 1].Selected = true;
                            Gan_Click();
                        }
                        if (vitri == 0)
                        {
                            NhanVienBUS.DeleteNhanVien(maNV);
                            Gan_Click();
                            grdList.CurrentCell = grdList.Rows[vitri + 1].Cells[0];
                            grdList.Rows[vitri + 1].Selected = true;
                            this.AddBlank();
                            Gan_Click();
                        }
                        if (vitri > 0 && vitri < grdList.RowCount)
                        {
                            NhanVienBUS.DeleteNhanVien(maNV);
                            this.AddBlank();
                            grdList.CurrentCell = grdList.Rows[vitri - 1].Cells[0];
                            grdList.Rows[vitri - 1].Selected = true;
                            Gan_Click();
                        }
                   
                }
            }
            catch (Exception ex)
            {
                // this.AddBlank();
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData.Equals(Keys.Escape))
            {
                this.keyValue = 0;
                if (!(this.plAddMode || this.plEditMode))
                {
                    this.Close();
                }
                else
                {
                    DialogResult dr = MessageBox.Show("Bạn có muốn đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dr == DialogResult.Yes)
                        this.Close();
                }
            }
            else if (keyData.Equals(Keys.F2))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Save();
                else
                    this.AddNew();

                this.ResetControl();
            }
            else if (keyData.Equals(Keys.F3))
            {
                if (!(this.plAddMode || this.plEditMode))
                {
                    if (string.IsNullOrEmpty(this.txtFind.Text.Trim()))
                    {
                        this.txtFind.Select();
                        this.txtFind.SelectAll();
                    }
                    else
                    {
                        //this.Find();
                    }
                }
            }
            else if (keyData.Equals(Keys.F4))
            {
                if ((this.plAddMode || this.plEditMode))
                    this.Undo();
                else
                    this.Edit();
                this.ResetControl();
            }
            else if (keyData.Equals(Keys.Control | Keys.D))
            {
                if (!(this.plAddMode || this.plEditMode))
                    this.Delete();
                this.ResetControl();
            }
            //else if (keyData.Equals(Keys.Control | Keys.P))
            //{
            //    if (!(this.plAddMode || this.plEditMode))
            //        //this.Preview();
            //}
            return base.ProcessDialogKey(keyData);
        }
        public void Gan_Click()
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void grdList_Click(object sender, EventArgs e)
        {

        }

        private void grdList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (this.grdList.SelectedRows.Count == 0)
                    return;
                DataGridViewRow rs = this.grdList.SelectedRows[0];
                GanDK(rs);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi trong quá trình thực hiện");

            }
        }

        private void cmdFirst_Click(object sender, EventArgs e)
        {
            dsNV.MoveFirst();
        }

        private void cmdPrev_Click(object sender, EventArgs e)
        {
            dsNV.MovePrevious();
        }

        private void cmdNext_Click(object sender, EventArgs e)
        {
            dsNV.MoveNext();
        }

        private void cmdLast_Click(object sender, EventArgs e)
        {
            dsNV.MoveLast();
        }

        private void cmdEdit_Click(object sender, EventArgs e)
        {
            if (this.plAddMode || this.plEditMode)
            {
                this.Undo();
                this.HienThiDanhSach();
            }
            else
            {
               
                this.Edit();
            }
            this.HienThiDanhSach();
            this.ResetControl();
        }

        private void cmdDel_Click(object sender, EventArgs e)
        {
            this.Delete();
            HienThiDanhSach();
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtFind_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtFind.Text.Trim()=="")
            {
                this.HienThiDanhSach();
            }
            else
            {
                dsNV.DataSource = NhanVienBUS.SearchNhanVienCondition(txtFind.Text);
                grdList.DataSource = dsNV;
            }
            
        }
    }
}